CREATE PROCEDURE Right_FindAll
AS
BEGIN
    Select 
    RightRowId,
    ApplicationId,
    RightName
    from Right
END
GO

CREATE PROCEDURE Right_FindById
@RightRowId int
AS
BEGIN
    Select * from Right where RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_FindByName
@name nvarchar(50)
AS
BEGIN
    Select * from Right where RightRowId = @name
END
GO

CREATE PROCEDURE Right_Insert
        @RightRowId int ,
        @ApplicationId int ,
        @RightName nvarchar (100)
AS
BEGIN
INSERT INTO Right
(
        RightRowId,
        ApplicationId,
        RightName
)
VALUES
(
        @RightRowId,
        @ApplicationId,
        @RightName
)
select @RightRowId = @@IDENTITY
END
GO

CREATE PROCEDURE Right_Update
        @RightRowId int ,
        @ApplicationId int ,
        @RightName nvarchar (100),
AS
BEGIN
UPDATE  Right SET
    ApplicationId = @ApplicationId,
    RightName = @RightName
    WHERE RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_Delete
    @RightRowId int
AS
BEGIN
    DELETE FROM Right where RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_Paging
    @PageSize int, 
    @PageNumber int
    AS 
    declare @RowStart int
    declare @RowEnd int
    if @PageNumber > 0 
    BEGIN	
    set @PageNumber = @PageNumber - 1
    set @RowStart = @PageSize * @PageNumber + 1;
    set @RowEnd = @RowStart  + @PageSize - 1;
    SELECT TotalRowCount = COUNT(RightRowId) FROM Right;
    WITH MyTable as 
    (
    SELECT * ,  ROW_NUMBER() OVER (order by SortIndex) as RowNumber From  Right
    )
    SELECT * from MyTable where RowNumber >= @RowStart and RowNumber <= @RowEnd
END
GO

CREATE proc Right_SetActive
@RightRowId int
AS
declare @Type bit
SELECT @Type =  IsActive from Right where RightRowId = @RightRowId
BEGIN
IF(@Type = 1)
Update Right set IsActive = 0 where RightRowId = @RightRowId
else
Update Right set IsActive = 1 where RightRowId = @RightRowId
END
GO

CREATE proc Right_SetSortIndex
@RightRowId int, 
@Type int
AS
declare @CurrentSort int
select @CurrentSort = SortIndex from  Right where RightRowId = @RightRowId
BEGIN
IF(@Type = 1)
Update Right set SortIndex =  @CurrentSort + 1 where RightRowId = @RightRowId
else
Update Right set SortIndex =  @CurrentSort - 1 where RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_RemoveSelectedItem
    @RightRowId int
AS
BEGIN
    UPDATE Right SET IsDeleted = 1 WHERE RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_RestoreSelectedItem
    @RightRowId int
AS
BEGIN
    UPDATE Right SET IsDeleted = 0 WHERE RightRowId = @RightRowId
END
GO

CREATE PROCEDURE Right_RemoveAll
    @RightRowId int
AS
BEGIN
    UPDATE Right SET IsDeleted = 1
END
GO

CREATE PROCEDURE Right_RestoreAll
    @RightRowId int
AS
BEGIN
    UPDATE Right SET IsDeleted = 0
END
GO

